// pages/goodsDetails/goodsDetails.js
//商品详情 包括限时抢购
let common_js = require("../../utils/common.js");
let url_js = require("../../utils/url.js");
let user_js = require("../../utils/user.js");
let sys_js = require("../../utils/system.js");
let page_js = require("../../utils/pageOptions.js");
let cart_js = require("../../utils/cart.js");
let ubt_js = require("../../utils/ubt.js");
let name_js = require("../../utils/ubtButtonName.js");
let api = require("../../utils/apiRequest.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    sysInfo: null,
    userInfo: null,
    productId: '',
    shareShopId: '',
    userId: '',
    groupId: '', //以上三个字段需要参与分享相关
    shareZtdInfo: '', //来自分享的自提点
    contentStatus: -1, //1有数据
    pageScrollTop: 0,
    containerTouchmove: '',
    buyLimitStatus: 1, //0即将开始，1进行中
    isKtuanBtnShow: false, //分享形式进来，根据判断显示我要开团
    isGroupBuyingAllMembersShow: false, //该商品拼团人员
    isShareViewShow: false, //分享视图
    isSelectZtdShow: false, //分享自提点显示
    shareSelectZtdIndex: 0, //分享自提点不同选择
    goodsPropertyInfo: {
      pageShow: false, //页面展示
      dismiss: false, //手动设置隐藏，当属性页面隐藏完成后，pageShow设置false
    },
    isSharePageShow: false, //生成分享海报
    goodsDetailInfo: {}, //商品详情对象
    gbPersonShowingList: [], //展示的拼团列表
    gbPersonAllList: [], //展示更多的拼团列表
    bannerIndex: 0,
    limitTimerObj: { //限时抢购计时器对象
      timer: null,
      isEnd: false,
      endTime: '',
      item: null
    },
    syncTimerObj: { //一直循环的计时器，用来展示所有拼团的时候保持跟展示的倒计时一致
      timer: null,
      second: 0,
    },
    sharePageInfo: { //分享
      iconFilePath: '',
      erCodeData: ''
    },
    selecteSku: null,
    cartCount: 0, //购物车数量
    promotionPrice: '', //促销价格
    price: null, //原价
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //业务埋点
    ubt_js.myPageView(common_js.currentPageName(), options);
    //0.禁止转发
    wx.hideShareMenu();
    //初始化数据
    this.initData(options);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //如果是分享进来的，并且没有选择角色，进入角色选择页面
    if (page_js.pageOptionsInfo()) {
      //2.relauch
      wx.reLaunch({
        url: '../indexSearchShop/indexSearchShop'
      });
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    //如果计时器的话清除
    if (this.data.limitTimerObj.timer) {
      clearTimeout(this.data.limitTimerObj.timer);
    }
    if (this.data.syncTimerObj.timer) {
      clearTimeout(this.data.syncTimerObj.timer);
    }
    // 业务埋点
    ubt_js.myPageViewOut(common_js.currentPageName());
  },


  //初始化options数据
  initUnShareOptionsData: function (options) {
    //userInfo
    let tempUserInfo = user_js.userInfo();
    //productId
    let tmpProductId = options.productId ? options.productId : '';
    if (!tmpProductId) {
      common_js.showToast("商品不存在！");
      return
    }

    //set data
    this.setData({
      productId: tmpProductId,
      userInfo: tempUserInfo,
      sysInfo: sys_js.systemInfo(),
      cartCount: cart_js.count()
    });
  },

  //初始化数据
  initData: function (options) {
    //1.正常进入
    let tmpProductId = '';
    let tmpShareShopId = '';
    let tmpUserId = '';
    let tmpGroupId = '';
    if (!options.scene) {
      tmpProductId = options.productId ? options.productId : '';
      tmpShareShopId = options.shareShopId ? options.shareShopId : ''; //分享过来，必然有
      tmpUserId = options.userId ? options.userId : ''; //分享过来，必然有
      tmpGroupId = options.groupId ? options.groupId : ''; //只有邀请拼团的时候有
    } else {
      //2.扫码进入
      // { productId }, { groupId }, { userId }, { shareShopid }
      let scene = decodeURIComponent(options.scene);
      let tmpArr = scene.split(',');
      tmpProductId = tmpArr[0] ? tmpArr[0] : '';
      tmpShareShopId = tmpArr[3] ? tmpArr[3] : ''; //分享过来，必然有
      tmpUserId = tmpArr[2] ? tmpArr[2] : ''; //分享过来，必然有
      tmpGroupId = tmpArr[1] ? tmpArr[1] : ''; //只有邀请拼团的时候有
    }

    //本地信息
    let tmpUserInfo = user_js.userInfo();
    //1.分享进来
    if (tmpShareShopId) {
      if (tmpGroupId) {
        //1.2.1未登录--->角色选择
        if (!user_js.hasRole()) {
          //1.save param
          let obj = {
            url: '../goodsDetails/goodsDetails',
            param: '?productId=' + tmpProductId + '&shareShopId=' + tmpShareShopId + '&userId=' + tmpUserId + '&groupId=' + tmpGroupId
          }
          page_js.savePageOptionsInfo(obj);
          //2.relauch
          //*********跳转放在onshow
          return;
        }
        //1.2拼团详情
        this.setData({
          userId: tmpUserId,
          shareShopId: tmpShareShopId,
          productId: tmpProductId,
          groupId: tmpGroupId,
          isKtuanBtnShow: true,
          userInfo: tmpUserInfo,
          sysInfo: sys_js.systemInfo(),
          cartCount: cart_js.count()
        });
        this.requestToGetProductInfo();
      } else {
        //1.2正常商品分享进来，userId、shareShopId存在
        //1.2.1没有角色--->角色选择
        if (!user_js.hasRole()) {
          //1.save param
          let obj = {
            url: '../goodsDetails/goodsDetails',
            param: '?productId=' + tmpProductId + '&shareShopId=' + tmpShareShopId + '&userId=' + tmpUserId + '&groupId=' + tmpGroupId
          }
          page_js.savePageOptionsInfo(obj);
          //2.relauch
          //*********跳转放在onshow
          return;
        }
        //1.2.2已登录绑定非分享所待自提点---->打开商品详情页，可选择分享所待点位，或选择当前已绑定点位
        if (tmpUserInfo && tmpUserInfo.ztd && (tmpUserInfo.ztd.id != tmpShareShopId)) {
          this.setData({
            userId: tmpUserId,
            shareShopId: tmpShareShopId,
            productId: tmpProductId,
            userInfo: tmpUserInfo,
            sysInfo: sys_js.systemInfo(),
            cartCount: cart_js.count()
          });
          this.requestToGetProductInfo(true);
          return;
        }
        //1.2.3已登录绑定分享所带自提点/或者未登录---->直接打开商品详情页
        if (tmpUserInfo && tmpUserInfo.ztd && (tmpUserInfo.ztd.id == tmpShareShopId)) {
          this.setData({
            userId: tmpUserId,
            shareShopId: tmpShareShopId,
            productId: tmpProductId,
            userInfo: tmpUserInfo,
            sysInfo: sys_js.systemInfo(),
            cartCount: cart_js.count()
          });
          this.requestToGetProductInfo();
        }
      }
    } else {
      //不是分享进来
      this.initUnShareOptionsData(options);
      //请求数据展示商品详情*********因为有分享，所以请求放在在这
      this.requestToGetProductInfo();
    }
  },

  //请求数据展示商品详情
  requestToGetProductInfo: function (isSelectZtdShow) {
    let data = {
      cityId: this.data.userInfo.ztd.cityId,
      code: user_js.getCode(),
      productId: this.data.productId,
      shopId: this.data.userInfo.ztd.id,
      shareShopId: this.data.shareShopId, //shopId存在，证明是分享页面进来,
      userId: this.data.userId, //userId存在，证明是分享页面进来,
      groupId: this.data.groupId //groupId存在，证明是分享拼团进来
    }
    api.wxRequest(url_js.productInfoUrl, data).then(res => {
      this.handleProductInfoResponseData(res, isSelectZtdShow);
    }).catch(err => {
      common_js.log(err);
    });
  },

  //处理返回的商品详情数据
  handleProductInfoResponseData: function (res, isSelectZtdShow) {
    if (res.code == 700) {
      // 商品不在该点位体售卖
      common_js.showModal("您当前所在点位没有售卖该商品", null, false, null, null, () => {
        wx.switchTab({
          url: '/pages/index/index'
        });
      })
      return;
    }
    if (!res.result) return;

    //stateText
    let tmpStateText = '';
    if (res.result.state == 0) {
      tmpStateText = "已下架";
    } else if (res.result.state == 2 || !res.result.stock) {
      tmpStateText = "已抢光";
    }
    res.result.stateText = tmpStateText;

    // 限时抢购或者是新人专享
    if ((res.result.type == 2 || res.result.type == 3) && res.result.expire > 0) {
      let tmpLimitTimerObj = this.data.limitTimerObj;
      tmpLimitTimerObj.endTime = new Date().getTime() + res.result.expire * 1000;
      this.setData({
        limitTimerObj: tmpLimitTimerObj,
      });
      this.countDownLimitTimer();
    }

    //当前选择的sku
    let tmpSelecteSku = {};
    if (res.result.skuList && res.result.skuList.length > 1) {
      //展示当前商品的价格
      tmpSelecteSku.image = res.result.mainImage;
      tmpSelecteSku.promotionPrice = res.result.minPromotionPrice;
      tmpSelecteSku.price = res.result.minPrice;
    } else {
      tmpSelecteSku = res.result.skuList[0];
    }

    //价格展示
    let tmpPromotionPrice = '';
    let tmpPrice = this.data.price;

    if (res.result.type == 0) {
      //只有原价
      tmpPromotionPrice = res.result.minPrice;
      tmpPrice = res.result.minCrossedPrice;
    } else {
      //促销或者团购的话有原价和促销价
      tmpPromotionPrice = res.result.minPromotionPrice;
      tmpPrice = res.result.minPrice;
    }

    //如果是拼团产品，展示的已经开团的信息
    let tmpGbPersonShowingList = [];
    let tmpGbPersonAllList = [];
    if (res.result.type == 1) {
      //拼团
      if (res.result.currentGroupList && res.result.currentGroupList.length > 0) {
        let tmpGroupList = res.result.currentGroupList;
        for (let i = 0; i < tmpGroupList.length; i++) {
          let item = tmpGroupList[i];
          if (i < 3) {
            tmpGbPersonShowingList.push(item);
          }
          tmpGbPersonAllList.push(item);
        }
      }
      //开始synctimer 
      this.countDownSyncTimer();
    }

    //set data
    if (isSelectZtdShow) {
      let tmpShareZtdInfo = this.data.shareZtdInfo;
      tmpShareZtdInfo = res.result.shareShop;
      this.setData({
        goodsDetailInfo: res.result,
        selecteSku: tmpSelecteSku,
        promotionPrice: tmpPromotionPrice,
        price: tmpPrice,
        gbPersonShowingList: tmpGbPersonShowingList,
        gbPersonAllList: tmpGbPersonAllList,
        shareZtdInfo: tmpShareZtdInfo,
        isSelectZtdShow: true,
        containerTouchmove: 'preventTouchMove',
        contentStatus: 1
      })
    } else {
      this.setData({
        goodsDetailInfo: res.result,
        selecteSku: tmpSelecteSku,
        promotionPrice: tmpPromotionPrice,
        price: tmpPrice,
        gbPersonShowingList: tmpGbPersonShowingList,
        gbPersonAllList: tmpGbPersonAllList,
        contentStatus: 1
      });
    }

    //1.开启转发
    wx.showShareMenu({});
  },

  //选择自提点，将刚才的自提点信息返回
  requestToSelectShop: function (shopId, geoLat, geoLon) {
    let data = {
      code: this.data.userInfo.code,
      shopId: shopId,
      geoLat: geoLat,
      geoLon: geoLon
    }
    api.wxRequest(url_js.shopSelectShopUrl, data).then(res => {
      this.handleSelectShopData(res);
    }).catch(err => {
      common_js.log(err);
    });
  },

  //处理选中自提点返回的数据
  handleSelectShopData: function (res, userInfo) {
    //1.保存当前用户自提点
    let tempUserInfo = user_js.userInfo();
    tempUserInfo.ztd = this.data.shareZtdInfo;
    user_js.saveUserInfo(tempUserInfo);
    this.setData({
      userInfo: tempUserInfo
    })
    //2.clear
    this.clearShareIdAndSoOn();
    //3.重新刷新页面
    this.requestToGetProductInfo();
  },

  //按钮点击事件
  //禁止滚动
  preventTouchMove: function () {},

  // 监听用户滑动页面事件
  onPageScroll: function (e) {
    let topY = e.scrollTop + "px";
    this.setData({
      pageScrollTop: topY
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    //埋点
    ubt_js.myClickButton(common_js.currentPageName(), name_js.goodsDetailsShare);

    return this.shareAppMessage();
  },

  //获取分享的AppMessage
  shareAppMessage: function () {
    let tmpPath = '/pages/goodsDetails/goodsDetails' + '?productId=' + this.data.goodsDetailInfo.id + '&shareShopId=' + this.data.userInfo.ztd.id + '&userId=' + (this.data.userInfo.userInfo ? this.data.userInfo.userInfo.id : '');
    let tmpTitle = "";
    if (this.data.goodsDetailInfo.type == 0) {
      tmpTitle = '【¥' + this.data.goodsDetailInfo.minPrice + '】' + this.data.goodsDetailInfo.title;
    } else {
      tmpTitle = '【促销价¥' + this.data.goodsDetailInfo.minPromotionPrice + '】' + '原价¥' + this.data.goodsDetailInfo.minPrice + '  ' + this.data.goodsDetailInfo.title;
    }
    return {
      title: tmpTitle ? tmpTitle : '好想你心选',
      path: tmpPath,
      imageUrl: this.data.goodsDetailInfo.mainImage
    }
  },

  //点击banner
  onBanner: function (e) {},

  //banner 改变
  onSwiperChange: function (e) {
    let index = e.detail.current;
    this.setData({
      bannerIndex: index
    });
  },

  //分享
  onShare: function (e) {
    //1.check login
    if (!this.checkIsToLoginIndex()) return;

    this.setData({
      isShareViewShow: true,
      containerTouchmove: 'preventTouchMove'
    });
  },

  // 去首页
  onHome() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },

  //关闭分享弹窗
  onShareViewClose: function (e) {
    this.setData({
      isShareViewShow: false,
      containerTouchmove: ''
    });
  },

  //已开团更多
  onGroupBuyingStartedMore: function (e) {
    this.setData({
      isGroupBuyingAllMembersShow: true,
      containerTouchmove: 'preventTouchMove'
    });
  },

  //关闭正在拼团的人员显示
  onGroupBuyingMembersClose: function (e) {
    this.setData({
      isGroupBuyingAllMembersShow: false,
      containerTouchmove: ''
    });
  },

  //已开团拼单
  onGroupBuyingStartedPd: function (e) {
    //1.check login
    if (!this.checkIsToLoginIndex()) return;

    let index = e.currentTarget.dataset.index;
    let item = this.data.gbPersonShowingList[index];
    //设置开始
    let tmpGoodsPropertyInfo = this.data.goodsPropertyInfo;
    tmpGoodsPropertyInfo.pageShow = true;
    //设置groupId
    let tmpGoodsInfo = this.data.goodsDetailInfo;
    tmpGoodsInfo.groupId = item.groupId;
    tmpGoodsInfo.isFqpt = true;
    tmpGoodsInfo.ptType = "2";
    this.setData({
      goodsDetailInfo: tmpGoodsInfo,
      goodsPropertyInfo: tmpGoodsPropertyInfo,
      containerTouchmove: 'preventTouchMove'
    });
  },

  //正在拼单的拼单按钮
  onGroupBuyingAllMembersPd: function (e) {
    //1.check login
    if (!this.checkIsToLoginIndex()) return;

    let index = e.currentTarget.dataset.index;
    let item = this.data.gbPersonAllList[index];
    //设置开始
    let tmpGoodsPropertyInfo = this.data.goodsPropertyInfo;
    tmpGoodsInfo.isFqpt = true;
    tmpGoodsPropertyInfo.pageShow = true;
    //设置groupId
    let tmpGoodsInfo = this.data.goodsDetailInfo;
    tmpGoodsInfo.groupId = item.groupId;
    tmpGoodsInfo.ptType = "2";
    this.setData({
      goodsDetailInfo: tmpGoodsInfo,
      goodsPropertyInfo: tmpGoodsPropertyInfo,
      containerTouchmove: 'preventTouchMove'
    });
  },

  //单独购买
  onBuyAlone: function () {
    //1.check login
    if (!this.checkIsToLoginIndex()) return;

    //2.展示多规格商品
    let tmpGoodsPropertyInfo = this.data.goodsPropertyInfo;
    tmpGoodsPropertyInfo.pageShow = true;
    let tmpInfo = this.data.goodsDetailInfo;
    tmpInfo.ptType = "1";
    this.setData({
      goodsDetailInfo: tmpInfo,
      goodsPropertyInfo: tmpGoodsPropertyInfo,
      containerTouchmove: 'preventTouchMove'
    });

    //埋点
    ubt_js.myClickButton(common_js.currentPageName(), name_js.goodsDetailsTuanBuyAlone);
  },

  //查看登录状态，未登录直接跳转登录
  checkIsToLoginIndex: function () {
    if (!user_js.isLogin()) {
      wx.navigateTo({
        url: '../loginIndex/loginIndex'
      });
      return false;
    }
    return true;
  },

  //发起拼团
  onBuyPt: function () {
    //1.check login
    if (!this.checkIsToLoginIndex()) return;

    //2.展示多规格商品
    let tmpGoodsPropertyInfo = this.data.goodsPropertyInfo;
    tmpGoodsPropertyInfo.pageShow = true;
    let tmpInfo = this.data.goodsDetailInfo;
    tmpInfo.isFqpt = true;
    tmpInfo.ptType = "2";
    this.setData({
      goodsDetailInfo: tmpInfo,
      goodsPropertyInfo: tmpGoodsPropertyInfo,
      containerTouchmove: 'preventTouchMove'
    });

    //埋点
    ubt_js.myClickButton(common_js.currentPageName(), name_js.goodsDetailsFqPt);
  },

  //加入购物车
  onAddCart: function (e) {
    //1.check login
    if (!this.checkIsToLoginIndex()) return;

    let item = this.data.goodsDetailInfo;
    if (item.skuList && item.skuList.length > 1) {
      if (item.dimensions.length == 0) {
        common_js.showToast("商品多规格属性不存在！");
        return;
      }
      //多规格,选择规格加入购物车
      //展示多规格商品
      let tmpGoodsPropertyInfo = this.data.goodsPropertyInfo;
      tmpGoodsPropertyInfo.pageShow = true;
      this.setData({
        goodsPropertyInfo: tmpGoodsPropertyInfo,
        containerTouchmove: 'preventTouchMove'
      });
    } else {
      //单一规格,直接添加购物车
      if (item.skuList == 0) {
        common_js.showToast("商品sku不存在");
        return;
      }
      //限时抢购单独判断
      if (item.type != 0) {
        let skuItem = item.skuList[0];
        let cartLimitCount = cart_js.limitSkuCountWithSkuId(skuItem.id);
        let isAlert = (cartLimitCount == 0) ? (item.count > skuItem.userBuyLimit) : (cartLimitCount >= skuItem.userBuyLimit);
        if (isAlert) {
          common_js.showModal("每人限购" + skuItem.userBuyLimit + "件，超过的件数将以原价支付！", null, true, null, "我知道了", () => {
            this.requestToAddCart(item.skuList[0], item.id, item.skuList[0].id, 1);
          })
          return;
        }
      }
      this.requestToAddCart(item.skuList[0], item.id, item.skuList[0].id, 1);
    }

    //埋点
    ubt_js.myClickButton(common_js.currentPageName(), name_js.goodsDetailsAdd);
  },

  // 立即购买
  onBuyRightNow() {
    //1.check login
    if (!this.checkIsToLoginIndex()) return;

    //2.展示多规格商品
    let tmpGoodsPropertyInfo = this.data.goodsPropertyInfo;
    tmpGoodsPropertyInfo.pageShow = true;
    let tmpGoodsInfo = this.data.goodsDetailInfo;
    tmpGoodsInfo.isBuyRightNow = true;
    this.setData({
      goodsDetailInfo: tmpGoodsInfo,
      goodsPropertyInfo: tmpGoodsPropertyInfo,
      containerTouchmove: 'preventTouchMove'
    });

    //埋点
    ubt_js.myClickButton(common_js.currentPageName(), name_js.goodsDetailsLjgm);
  },

  //增加购物车
  requestToAddCart: function (skuItem, productId, skuId, num) {
    let data = {
      cityId: this.data.userInfo.ztd.cityId,
      code: user_js.getCode(),
      num: num,
      productId: productId,
      shopId: this.data.userInfo.ztd.id,
      skuId: skuId
    };
    api.wxRequest(url_js.cartAddUrl, data).then(res => {
      this.handleAddCartData(res, skuItem, skuId, num);
    }).catch(err => {
      common_js.log(err);
    });
  },

  //处理添加购物车
  handleAddCartData: function (res, skuItem, skuId, num) {
    cart_js.addCart(skuItem, skuId, num);
    this.setData({
      cartCount: (this.data.cartCount + num)
    });
    common_js.showToast("添加购物车成功");
  },

  //跳转购物车
  onCart: function () {
    //埋点
    ubt_js.myClickButton(common_js.currentPageName(), name_js.goodsDetailsCart);

    //跳转购物车
    wx.switchTab({
      url: '/pages/cart/cart'
    });
  },


  //分享过来，我要拼团
  onWykt: function (e) {
    //1.判断登录
    if (!this.checkIsToLoginIndex()) return;

    //设置开始
    let tmpGoodsPropertyInfo = this.data.goodsPropertyInfo;
    tmpGoodsPropertyInfo.pageShow = true;
    //设置groupId
    let tmpGoodsInfo = this.data.goodsDetailInfo;
    tmpGoodsInfo.groupId = this.data.groupId;
    this.setData({
      goodsDetailInfo: tmpGoodsInfo,
      goodsPropertyInfo: tmpGoodsPropertyInfo,
      containerTouchmove: 'preventTouchMove'
    });
  },

  //分享微信好友
  onShareWechat: function (e) {
    //判断是否登录,未登录则不显示店名和地址 
    this.setData({
      isShareViewShow: false,
      containerTouchmove: 'preventTouchMove'
    });
  },

  //分享生成海报
  onSharePage: function (e) {
    //判断是否登录,未登录则不显示店名和地址
    //1.check
    if (!this.data.goodsDetailInfo.mainImage) {
      common_js.showToast("商品主图不存在!");
      return;
    }

    //2.下载分享所需要的图片
    if (this.data.sharePageInfo.iconFilePath && this.data.sharePageInfo.erCodeData) {
      this.setData({
        isShareViewShow: false,
        isSharePageShow: true,
        containerTouchmove: 'preventTouchMove'
      })
      return;
    }

    //3.show
    this.setData({
      isShareViewShow: false,
      containerTouchmove: 'preventTouchMove'
    });
    //4.下载
    this.downloadSharePageImages();
    //埋点
    ubt_js.myClickButton(common_js.currentPageName(), name_js.goodsDetailsSchb);
  },

  //下载分享所需要的图片
  downloadSharePageImages: function () {
    let generateCodeData = {
      cityId: this.data.userInfo.ztd.cityId,
      code: user_js.getCode(),
      page: 'pages/goodsDetails/goodsDetails',
      productId: this.data.productId,
      shopId: this.data.userInfo.ztd.id
    }
    //1.下载图片
    Promise.all([api.wxDownloadFile(this.data.goodsDetailInfo.mainImage), api.wxRequest(url_js.productGenerateCodeUrl, generateCodeData)]).then(results => {
      this.handleDownloadSharePageImagesResponseData(results);
    }).catch(err => {
      common_js.log(err);
    });
  },

  //处理下载分享所需要的图片
  handleDownloadSharePageImagesResponseData: function (res) {
    //base64 imae
    let mainImgFilePath = res[0];
    let base64CodeData = res[1].result;

    let tmpSharePageInfo = this.data.sharePageInfo;
    tmpSharePageInfo.iconFilePath = mainImgFilePath;
    tmpSharePageInfo.erCodeData = base64CodeData;
    //
    this.setData({
      sharePageInfo: tmpSharePageInfo,
      isSharePageShow: true,
      containerTouchmove: 'preventTouchMove'
    })
  },

  //关闭选择自提点
  onCloseZtd: function (e) {
    this.setData({
      isSelectZtdShow: false,
      containerTouchmove: ''
    });
  },

  //确定自提点
  onZtdEnsure: function (e) {
    this.onCloseZtd();
    //reload data
    if (this.data.shareSelectZtdIndex == 1) {
      //clear
      this.clearShareIdAndSoOn();
      //重新刷新页面
      this.requestToGetProductInfo();
      return;
    };
    //根据选择的自提点切换选择的自提点
    this.requestToSelectShop(this.data.shareZtdInfo.id, this.data.shareZtdInfo.geoLat, this.data.shareZtdInfo.geoLon);
  },

  //清空分享带过来的信息
  clearShareIdAndSoOn: function () {
    this.setData({
      userId: '',
      shareShopId: ''
    })
  },

  //分享选择自提点
  onShareZtdSelect: function (e) {
    let index = parseInt(e.currentTarget.dataset.index);
    this.setData({
      shareSelectZtdIndex: index
    })
  },

  //商品属性选择确定
  onGoodsPropertyEnsure: function (e) {
    //消失
    this.onGoodsPropertyHidden();
    //添加购物车
    if (e) {
      let item = e.detail;
      if (this.data.goodsDetailInfo.type == 1) {
        //团购
        let tmpSku = item.sku;
        tmpSku.productId = this.data.goodsDetailInfo.id;
        tmpSku.skuId = tmpSku.id;
        tmpSku.num = item.count;
        let skuList = [tmpSku];
        //ptType拼团
        let tmpUrl = '../payment/payment?skuList=' + common_js.jsonStringify(skuList);
        if (item.groupId) {
          tmpUrl = tmpUrl + '&groupId=' + item.groupId;
        }
        if (item.isFqpt) {
          tmpUrl = tmpUrl + '&isFqpt=' + item.isFqpt
        }
        if (item.ptType) {
          tmpUrl = tmpUrl + '&ptType=' + item.ptType
        }
        wx.navigateTo({
          url: tmpUrl
        });
        return;
      } else if (this.data.goodsDetailInfo.type == 0 && item.isBuyRightNow) {
        // 正常商品立即购买
        let tmpSku = item.sku;
        tmpSku.productId = this.data.goodsDetailInfo.id;
        tmpSku.skuId = tmpSku.id;
        tmpSku.num = item.count;
        let skuList = [tmpSku];
        let tmpUrl = '../payment/payment?skuList=' + common_js.jsonStringify(skuList);
        wx.navigateTo({
          url: tmpUrl
        });
        return;
      } else if (this.data.goodsDetailInfo.type != 0) {
        //限时抢购单独判断
        let skuItem = item.sku;
        let cartLimitCount = cart_js.limitSkuCountWithSkuId(skuItem.id);
        let isAlert = (cartLimitCount == 0) ? (item.count > skuItem.userBuyLimit) : (cartLimitCount >= skuItem.userBuyLimit);
        if (isAlert) {
          common_js.showModal("每人限购" + skuItem.userBuyLimit + "件，超过的件数将以原价支付！", null, true, null, "我知道了", () => {
            this.limitGoodsEnsureBuy(item);
          })
        } else {
          this.limitGoodsEnsureBuy(item);
        }
        return;
      }
      this.requestToAddCart(item.sku, this.data.goodsDetailInfo.id, item.sku.id, item.count);
    }
  },

  // 立即购买----显示抢购
  limitGoodsEnsureBuy(item) {
    if (item.isBuyRightNow) {
      let tmpSku = item.sku;
      tmpSku.productId = this.data.goodsDetailInfo.id;
      tmpSku.skuId = tmpSku.id;
      tmpSku.num = item.count;
      let skuList = [tmpSku];
      let tmpUrl = '../payment/payment?skuList=' + common_js.jsonStringify(skuList);
      wx.navigateTo({
        url: tmpUrl
      });
    } else {
      this.requestToAddCart(item.sku, this.data.goodsDetailInfo.id, item.sku.id, item.count);
    }
  },

  //商品属性选择
  onGoodsPropertyHidden: function (e) {
    let tmpGoodsPropertyInfo = this.data.goodsPropertyInfo;
    tmpGoodsPropertyInfo.pageShow = false;
    tmpGoodsPropertyInfo.dismiss = false;
    let tmpGoodsInfo = this.data.goodsDetailInfo;
    if (tmpGoodsInfo.groupId) {
      delete tmpGoodsInfo.groupId;
    }
    if (tmpGoodsInfo.isFqpt) {
      delete tmpGoodsInfo.isFqpt;
    }
    if (tmpGoodsInfo.isBuyRightNow) {
      delete tmpGoodsInfo.isBuyRightNow;
    }

    this.setData({
      goodsDetailInfo: tmpGoodsInfo,
      containerTouchmove: '',
      goodsPropertyInfo: tmpGoodsPropertyInfo
    });
  },

  //隐藏商品属性页面
  onHidePropertyPageView: function (e) {
    let tmpGoodsPropertyInfo = this.data.goodsPropertyInfo;
    tmpGoodsPropertyInfo.dismiss = true;
    this.setData({
      goodsPropertyInfo: tmpGoodsPropertyInfo
    });
  },


  //关闭海报页面
  onPageClose: function (e) {
    this.setData({
      isSharePageShow: false,
      containerTouchmove: ''
    });
  },

  // 监听用户滑动页面事件
  onPageScroll: function (e) {
    let topY = e.scrollTop + "px";
    this.setData({
      pageScrollTop: topY
    });
  },

  // 万人团开始
  onWrtBegin() {
    let detial = this.data.goodsDetailInfo;
    detail.groupInfo.status = 2;
    this.setData({
      goodsDetailInfo: detail
    });
  },

  // 同步计时器**********
  countDownSyncTimer() { //倒计时函数
    //定时清除
    clearTimeout(this.data.syncTimerObj.timer);

    let tmpTimerObj = this.data.syncTimerObj;
    tmpTimerObj.second++;
    //1s调用一次
    let that = this;
    let timerItem = setTimeout(function () {
      that.countDownSyncTimer()
    }, 1000);
    tmpTimerObj.timer = timerItem;
    // 渲染，然后每隔一秒执行一次倒计时函数
    this.setData({
      syncTimerObj: tmpTimerObj
    })
  },
  // 同步计时器**********


  // 限时抢购计时器**********
  countDownLimitTimer() { //倒计时函数
    //定时清除
    clearTimeout(this.data.limitTimerObj.timer);
    // 获取当前时间，同时得到活动结束时间数组
    let newTime = new Date().getTime();
    let tmpTimerObj = this.data.limitTimerObj;
    let endTime = tmpTimerObj.endTime;
    let time = (endTime - newTime) / 1000;
    // 如果活动未结束，对时间进行处理
    let hmsObj = common_js.getHrMinSecBySecond(time);
    tmpTimerObj.item = hmsObj.item;
    tmpTimerObj.isEnd = hmsObj.isEnd;
    //1s调用一次
    let that = this;
    let timerItem = setTimeout(function () {
      that.countDownLimitTimer()
    }, 1000);
    if (tmpTimerObj.isEnd) {
      clearTimeout(timerItem);
      let tmpDetailInfo = this.data.goodsDetailInfo;
      tmpDetailInfo.activityState = 2; //结束
      this.setData({
        goodsDetailInfo: tmpDetailInfo
      });
      return;
    } else {
      tmpTimerObj.timer = timerItem;
    }
    // 渲染，然后每隔一秒执行一次倒计时函数
    this.setData({
      limitTimerObj: tmpTimerObj
    })
  }
  // 限时抢购计时器**********
})